<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    * {
        margin: 0;
        padding: 0;
    }

    .box {
        position: relative;
        margin: 50px auto;
        height: 400px;
        width: 512px;
        border: 1px solid black;
        /*超出部分隐藏*/
        overflow: hidden;
    }

    /* 谁动谁定位 */
    .box>img {
        position: absolute;
        top: 0;
        left: 0;
    }

    .box>span {
        position: absolute;

        left: 0;
        width: 100%;
        height: 200px;
        cursor: pointer;
    }

    .box>span#up {
        top: 0;
    }

    .box>span#down {
        bottom: 0;
    }
</style>

<body>
    <div class="box">
        <img src="./11滚动长图/imgs/mi.png" alt="">
        <!-- 辅助元素 -->
        <span id="up"></span>
        <span id="down"></span>
    </div>
</body>
<script>
    window.onload = function () {
        function $(selecter) {
            return document.querySelector(selecter);
        }
        var moveVal = 0;
        var timer = null;
        $('#up').onmouseover = function () {
            // 定时器使用前要先清除
            clearInterval(timer);
            timer = setInterval(() => {
                moveVal -= 3;
                if (moveVal <= -1070) {
                    clearInterval(timer);
                } else {
                    $('img').style.top = moveVal + 'px';
                }

            }, 30)

        }
        $('#down').onmouseover = function () {
            // 定时器使用前要先清除
            clearInterval(timer);
            timer = setInterval(() => {
                moveVal += 3;
                if (moveVal <= 0) {
                    $('img').style.top = moveVal + 'px';
                } else {
                    clearInterval(timer);
                }
            }, 30)
        }
        //鼠标离开大盒子
        $('.box').onmouseout = function () {
            clearInterval(timer);
        }
    }
</script>

</html>